import uuid
from django.db import models
from projects.userprofile.models import UserProfile


class Group(models.Model):
    """
    群组信息
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(null=True, blank=True, max_length=32, verbose_name='群组名称')
    creator = models.ForeignKey(to=UserProfile, related_name="creator",
                                on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="群主")
    member = models.ManyToManyField(to=UserProfile, verbose_name="成员")

    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        ordering = ['-create_time']
        db_table = 'group'
        verbose_name = '群组'
        verbose_name_plural = verbose_name


class GroupMessage(models.Model):
    """
    群消息
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    group = models.ForeignKey(to=Group, on_delete=models.DO_NOTHING, verbose_name="所属群")
    user = models.ForeignKey(to=UserProfile, on_delete=models.DO_NOTHING, verbose_name="谁发送的消息")
    message = models.CharField(max_length=255, verbose_name="消息")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='消息发送时间')

    class Meta:
        ordering = ['-create_time']
        db_table = 'group_message'

